<?php
/**
 * Created by JetBrains PhpStorm.
 * User: dodasso
 * Date: 7/14/13
 * Time: 8:10 PM
 * To change this template use File | Settings | File Templates.
 */

define('APP_ROOT', realpath(__DIR__ . '/../..'));

require_once(APP_ROOT . '/modules/common/Errors.php');
require_once(APP_ROOT . '/modules/common/Utils.php');
require_once(APP_ROOT . '/modules/common/SessionDto.php');
require_once(APP_ROOT . '/modules/persistance/DBConnector.php');
require_once(APP_ROOT . '/modules/persistance/SessionPersistance.php');

use persistance\DBConnector;
use persistance\SessionPersistance;
use common\Errors;
use common\Utils;
use common\SessionDto;

function testCreateSessionWithNonExistingKey() {
    $session                  = new SessionDto();
    $session->key             =  sha1(Utils::uuid());
    $session->remoteAddresses = '192.168.1.1';
    $session->userId          = 1;

    $dbconn = new DBConnector();
    $dbconn->connect();

    $p = new SessionPersistance();
    if(!$p->init($dbconn)) {
        print('testCreateSessionWithNonExistingKey(): FAIL' . PHP_EOL);
        return;
    }

    $rs = $p->createSession($session);

    print('testCreateSessionWithNonExistingKey(): ' .
        (($rs['error'] === Errors::INTEGRAL_ERR_NONE) ? 'OK' : 'FAIL') .
        PHP_EOL);
}

function testCreateSessionWithExistingKey() {
    $session                  = new SessionDto();
    $session->key             = '1234567890123456789012345678901234567890';
    $session->remoteAddresses = '192.168.1.1';
    $session->userId          = 1;

    $dbconn = new DBConnector();
    $dbconn->connect();

    $p = new MovementPersistance();
    if(!$p->init($dbconn)) {
        print('testCreateSessionWithExistingKey(): FAIL' . PHP_EOL);
        return;
    }

    $rs = $p->createSession($session);

    print('testCreateSessionWithExistingKey(): ' .
        (($rs['error'] === Errors::INTEGRAL_ERR_PERSISTANCE_UNABLE_TO_CREATE) ? 'OK' : 'FAIL') .
        PHP_EOL);
}

function testDeleteSessionWithNonExistingKey() {
    $session                  = new SessionDto();
    $session->id              = 0;
    $session->key             = sha1(Utils::uuid());
    $session->remoteAddresses = '192.168.1.1';
    $session->userId          = 1;

    $dbconn = new DBConnector();
    $dbconn->connect();

    $p = new MovementPersistance();
    if(!$p->init($dbconn)) {
        print('testDeleteSessionWithNonExistingKey(): FAIL' . PHP_EOL);
        return;
    }

    $rs = $p->deleteSession($session);

    print('testDeleteSessionWithNonExistingKey(): ' .
        (($rs['error'] === Errors::INTEGRAL_ERR_PERSISTANCE_UNABLE_TO_DELETE) ? 'OK' : 'FAIL') .
        PHP_EOL);
}

function testDeleteSessionWithExistingKey() {
    $session                  = new SessionDto();
    $session->key             = sha1(Utils::uuid());
    $session->remoteAddresses = '192.168.1.1';
    $session->userId          = 1;

    $dbconn = new DBConnector();
    $dbconn->connect();

    $p = new MovementPersistance();
    if(!$p->init($dbconn)) {
        print('testDeleteSessionWithExistingKey(): FAIL' . PHP_EOL);
        return;
    }

    $rs = $p->createSession($session);
    if($rs['error'] != Errors::INTEGRAL_ERR_NONE) {
        print('testDeleteSessionWithExistingKey(): FAIL');
        return;
    }

    $session = $rs['data'];
    $rs = $p->deleteSession($session);

    print('testDeleteSessionWithExistingKey(): ' .
        (($rs['error'] === Errors::INTEGRAL_ERR_NONE) ? 'OK' : 'FAIL') .
        PHP_EOL);
}

function testFindSessionByExistingKey() {
    $dbconn = new DBConnector();
    $dbconn->connect();

    $p = new MovementPersistance();
    if(!$p->init($dbconn)) {
        print('testFindSessionByExistingKey(): FAIL' . PHP_EOL);
        return;
    }

    $rs = $p->findByKey('1234567890123456789012345678901234567890');

    print('testFindSessionByExistingKey(): ' .
        (($rs['error'] === Errors::INTEGRAL_ERR_NONE) ? 'OK' : 'FAIL') .
        PHP_EOL);
}

function testFindSessionByNotExistingKey() {
    $dbconn = new DBConnector();
    $dbconn->connect();

    $p = new MovementPersistance();
    if(!$p->init($dbconn)) {
        print('testFindSessionByNotExistingKey(): FAIL' . PHP_EOL);
        return;
    }

    $rs = $p->findByKey('9999999999');

    print('testFindSessionByNotExistingKey(): ' .
        (($rs['error'] === Errors::INTEGRAL_ERR_PERSISTANCE_NOT_FOUND) ? 'OK' : 'FAIL') .
        PHP_EOL);
}

testCreateSessionWithNonExistingKey();
testCreateSessionWithExistingKey();
testDeleteSessionWithNonExistingKey();
testDeleteSessionWithExistingKey();
testFindSessionByExistingKey();
testFindSessionByNotExistingKey();
